app.controller('goodsDetailAddCtrl', ['$scope', '$state', 'goodsDetailAddService', 'toaster', function ($scope, $state, goodsDetailAddService, toaster) {
    $scope.goodsDetailAddService = goodsDetailAddService;
    $scope.brands;
    $scope.types;
    $scope.skus = new Array();
    $scope.skuContents = new Array();
    $scope.systemTypesArr=[];
    $scope.technicianArr=[];

    $scope.weeks=[{name:'周一',state:false},{name:'周二',state:false},{name:'周三',state:false},{name:'周四',state:false},
        {name:'周五',state:false},{name:'周六',state:false},{name:'周日',state:false}];
    $scope.times=['0:00','0:30','1:00','1:30','2:00','2:30','3:00','3:30','4:00','4:30','5:00','5:30','6:00','6:30','7:00','7:30','8:00',
        '8:30','9:00','9:30','10:00','10:30','11:00','11:30','12:00','12:30','13:00','13:30','14:00','14:30','15:00','15:30','16:00',
        '16:30','17:00','17:30','18:00','18:30','19:00','19:30','20:00','20:30','21:00','21:30','22:00','22:30','23:00','23:30'];


    //查询店铺信息
    $scope.queryShop=function(){
        goodsDetailAddService.queryShop($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.shop=result.data;
                    goodsDetailAddService.goods=initGoods;
                    //设置默认商品分销类型
                    if($scope.shop.isChain==0 && $scope.shop.isPlatform==0){
                        if($scope.shop.isOpenSell==1){
                            goodsDetailAddService.goods.isOpenSell=$scope.shop.isOpenSell;
                            goodsDetailAddService.goods.profit=$scope.shop.profit;
                        }else if($scope.shop.isOpenRebate==1){
                            goodsDetailAddService.goods.isOpenRebate=$scope.shop.isOpenRebate;
                            if($scope.shop.profit<26){
                                goodsDetailAddService.goods.profit=26;
                            }else{
                                goodsDetailAddService.goods.profit=$scope.shop.profit;
                            }
                        }
                    }

                    //设置默认商品销售类型
                    if($scope.shop.isShop==1){
                        goodsDetailAddService.goods.type=0;
                        break;
                    }else if($scope.shop.isDistribution==1){
                        goodsDetailAddService.goods.type=1;
                        break;
                    }else if($scope.shop.isMail==1){
                        goodsDetailAddService.goods.type=2;
                        break;
                    }else if($scope.shop.isEat==1){
                        goodsDetailAddService.goods.type=3;
                        break;
                    }else if($scope.shop.isReserve==1){
                        goodsDetailAddService.goods.type=4;
                        break;
                    }
                    break;
                default :
                    break;
            }
        })
    }

    $scope.queryShop();


    //查询品牌信息
    $scope.querybrands = function () {
        goodsDetailAddService.querybrands($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.brands = result.data;
                    break;
                default :
                    break;
            }
        })
    }

    $scope.querybrands();

    //查询类型信息
    $scope.queryType = function () {
        goodsDetailAddService.queryType($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.types = result.data;
                    break;
                default :
                    break;
            }
        })
    }

    $scope.queryType();

    //查询系统分类
    $scope.querySystemType = function () {
        goodsDetailAddService.querySystemType($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.systemTypes = result.data;
                    break;
                default :
                    break;
            }
        })
    };

    $scope.querySystemType();

    $scope.changeSystemType=function(obj){
        if(obj.length > 0) {
            $scope.goodsDetailAddService.goods.systemTypeId=[];
            for(var i=0;i<obj.length;i++){
                $scope.goodsDetailAddService.goods.systemTypeId.push(obj[i].id);
            }
            $scope.getAll();
        }
    };


    //查询运费模板
    $scope.queryPostageTemplate = function () {
        goodsDetailAddService.queryPostageTemplate($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.postageTemplate = result.data;
                    break;
                default :
                    break;
            }
        })
    };

    $scope.queryPostageTemplate();


    $scope.changeSellType=function(type){
        if(type==0){
            goodsDetailAddService.goods.isOpenSell=0;
            goodsDetailAddService.goods.isOpenRebate=0;
            goodsDetailAddService.goods.profit=0;
        }else if(type==1){
            goodsDetailAddService.goods.isOpenSell=1;
            goodsDetailAddService.goods.isOpenRebate=0;
            goodsDetailAddService.goods.profit=$scope.shop.profit;
        }else if(type==2){
            goodsDetailAddService.goods.isOpenSell=0;
            goodsDetailAddService.goods.isOpenRebate=1;
            if($scope.shop.profit<26){
                goodsDetailAddService.goods.profit=26;
            }else{
                goodsDetailAddService.goods.profit=$scope.shop.profit;
            }
        } else if(type==3){
            goodsDetailAddService.goods.isOpenSell=1;
            goodsDetailAddService.goods.isOpenRebate=1;
            if($scope.shop.profit<26){
                goodsDetailAddService.goods.profit=26;
            }else{
                goodsDetailAddService.goods.profit=$scope.shop.profit;
            }
        }
    }


    //添加商品信息
    var initGoods={
        isOpenSku: 0,
        isFreeShipping: 0,
        isOpenMember: 0,
        isRecommend: 0,
        isOpenRebate: 0,
        isHelpPeasant: 0,
        postageType: 0,
        systemTypeId:[],
        isOpenSell:0,
        profit:0,
        postageWeight:0.000,
        tagIds:[],
        techniciansId:[]
    };

    //改变配送方式
    $scope.changeType=function(type){
        $scope.goodsDetailAddService.goods=initGoods;
        goodsDetailAddService.goods.type=type;
    };

    $scope.addState=false;

    $scope.addGoods = function () {
        if($scope.roundLabel()){
            return;
        }

        if($scope.addState){
            return;
        }

        $scope.addState=true;

        //参数体系
        goodsDetailAddService.goods.goodsLabelItems=roundLabelItems();

        //预约参数
        goodsDetailAddService.goods.reserveTime=goodsDetailAddService.goods.startTime+'-'+goodsDetailAddService.goods.endTime;
        goodsDetailAddService.goods.canReserve=selectWeek();

        goodsDetailAddService.addGoods($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.goodsId=result.data.id;
                    if($scope.goodsDetailAddService.goods.isOpenSku==1){
                        $scope.saveGoodsSku(result.data.id);
                    }else{
                        $state.go('app.goodsDetailPictures', {goodsId: result.data.id});
                        $scope.goodsDetailAddService.goods = initGoods;
                        $scope.addState=false;
                        toaster.pop('success', "提示", "操作成功");
                    }

                    break;
                case 624:
                case 711:
                    alert(result.message);
                    $scope.addState=false;
                    break;
                default :
                    $scope.addState=false;
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        })
    }

    $scope.saveGoodsSku=function(goodsId){
        goodsDetailAddService.saveGoodsSku($scope).then(function(result){
            switch (result.status) {
                case 200:
                    $state.go('app.goodsDetailPictures', {goodsId: goodsId});
                    $scope.goodsDetailAddService.goods = initGoods;
                    $scope.addState=false;
                    toaster.pop('success', "提示", "操作成功");
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });

    }


    $scope.$on('skuContents', function(event, data) {
        $scope.skuContents=data;
    });


    $scope.openSku=function(){
        if($scope.goodsDetailAddService.goods.isOpenSku==1){
            $scope.goodsDetailAddService.goods.price=0.00;
            $scope.goodsDetailAddService.goods.stock=0;
        }else if($scope.goodsDetailAddService.goods.isOpenSku==0){
            $scope.skus=[];
        }
    }



    //---参数区域块---
    //添加list
    $scope.addGoodslabelItems=function(){
        var goodsLabelItems=new Object();
        $scope.labels.push(goodsLabelItems);
    }

    //移除list
    $scope.removeGoodslabelItems=function(index){
        $scope.labels.splice(index, 1);
    }


    $scope.initLabel=function(item){
        item.showLabel=false;
        item.showLabelItems=false;
        item.label={};
    }

    $scope.initLabelItems=function(item){
        item.showLabelItems=!item.showLabelItems;
        item.showLabel=false;
    }

    $scope.labels=new Array();

    $scope.createLabel=function(item){
        if(item.label.name=="" || !item.label.name){
            return;
        }
        item.platform=0;
        $scope.addlabel(item);
        item.showLabel=false;
    }

    $scope.cancelLabel=function(item){
        item.label.name=null;
        item.showLabel=false;
    }


    $scope.createLabelItems=function(item){
        $scope.addlabelItems(item.labelItems);
        item.showLabelItems=false;
    }

    $scope.cancelLabelItems=function(item){
        item.labelItems.value=null;
        item.showLabelItems=false;
    }

    //改变区域
    $scope.changeLabel=function(em,item){
        item.labelItems=new Object();
        $scope.labelItemsList=em.childrens;
        item.showLabel=false;
        em.selected=false;
        item.label=em;
        item.labelItems.labelId=item.label.id;
    }

    $scope.changeLabelItems=function(em,item){
        item.labelItems=em;
        item.showLabelItems=false;
    }

    //
    var roundLabelItems=function(){
        var arr=new Array();
        for(var i=0;i<$scope.labels.length;i++){
            var obj=new Object();
            obj.labelId=$scope.labels[i].label.id;
            obj.labelItemsId=$scope.labels[i].labelItems.id;
            obj.sort=i;
            arr.push(obj);
        }

        return arr;
    }


    //
    var cancelAll=function(){
        for(var i=0;i<$scope.labels.length;i++){
            $scope.labels[i].showLabel=false;
            $scope.labels[i].showLabelItems=false;
        }
    }

    //整合labels
    $scope.integrationLabels=function(){
        var arr=new Array();

        for(var i=0;i<$scope.labels.length;i++){
            var id=$scope.labels[i].label.id;
        }
    }


    //添加标签参数
    $scope.addlabel=function(item){
        item.label.platform=0;
        item.label.systemTypeId=[];

        goodsDetailAddService.addlabel($scope,item.label).then(function(result){
            switch (result.status) {
                case 200:
                    item.label.id=result.data.id;
                    item.labelItems=new Object();
                    item.labelItems.labelId=item.label.id;
                    $scope.labelItemsList=new Array();
                    break;
                case 715:
                    alert("参数名字已经存在,请重新填写");
                    item.label=new Object();
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }


    //添加标签参数价值
    $scope.addlabelItems=function(labelItems){
        goodsDetailAddService.addlabelItems($scope,labelItems).then(function(result){
            switch (result.status) {
                case 200:
                    labelItems.id=result.data.id;
                    break;
                case 717:
                    alert("参数值已经存在,请重新填写");
                    labelItems=new Object();
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    //获取全部属性参数
    $scope.getAll=function(){
        goodsDetailAddService.getAll($scope).then(function(result){
            switch (result.status) {
                case 200:
                    $scope.labelList=result.data;

                    for(var i=0;i<$scope.labelList.length;i++){
                        $scope.labelList[i].selected=true;
                    }

                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    $scope.getAll();


    //addLabels
    $scope.roundLabel=function(){
        for(var i=0;i<$scope.labels.length;i++){
            if(!$scope.labels[i].label || !$scope.labels[i].labelItems || $scope.labels[i].label.name==null || $scope.labels[i].labelItems.value==null){
                alert("请填写完整的参数");
                return true;
            }
        }
        return false;
    }


    //查询标签
    $scope.getAllTags=function(){
        goodsDetailAddService.getAllTags($scope).then(function(result){
            switch (result.status) {
                case 200:
                    $scope.tags=result.data;
                    break;
                default :
                    toaster.pop('error', "操作失败", "发生未知错误！请联系管理员");
                    break;
            }
        });
    }

    $scope.getAllTags();

    //选择标签
    $scope.chooseItem=function(item,$event){
        if($event.target.checked){
            goodsDetailAddService.goods.tagIds.push(item.id);
        }else{
            var index=goodsDetailAddService.goods.tagIds.indexOf(item.id);
            if(index!=-1){
                goodsDetailAddService.goods.tagIds.splice(index,1);
            }
        }
    }

    //修改周期
    var selectWeek=function(){
        var day='';
        for(var i=0;i<$scope.weeks.length;i++){
            if($scope.weeks[i].state){
                day+=$scope.weeks[i].name+',';
            }
        }
        var index=day.lastIndexOf(',');
        if(index>-1){
            day=day.substring(0,index);
        }
        return day;
    }

    //查询店铺下的技师
    $scope.queryTechnician=function(){
        goodsDetailAddService.queryTechnician($scope).then(function (result) {
            switch (result.status) {
                case 200:
                    $scope.technicians = result.data;
                    break;
                default :
                    break;
            }
        })
    };
    $scope.queryTechnician();

    $scope.changeTechnician=function(obj){
        if(obj.length > 0) {
            $scope.goodsDetailAddService.goods.techniciansId = [];
            for (var i = 0; i < obj.length; i++) {
                $scope.goodsDetailAddService.goods.techniciansId.push(obj[i].id);
            }
        }
    }

}]);
